<template>
  <el-dialog v-model="visible" title="添加护理级别" width="400px" :close-on-click-modal="false"
    class="add-nursing-level-dialog">
    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" class="form-body">
      <el-form-item label="护理级别：" prop="levelName">
        <el-input v-model="form.levelName" placeholder="请输入护理级别名称" />
      </el-form-item>

      <el-form-item label="状态：" prop="status">
        <el-select v-model="form.status" placeholder="请选择">
          <el-option label="启用" value="1" />
          <el-option label="停用" value="0" />
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, defineExpose, defineEmits } from 'vue'
import {inject} from 'vue'
const axios = inject("axios")

import { ElMessage } from 'element-plus'

const visible = ref(false)
const formRef = ref()
const form = ref({
  levelName: '',
  status: ''
})

const rules = {
  levelName: [{ required: true, message: '请输入护理级别名称', trigger: 'blur' }],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }]
}

const emit = defineEmits(['refresh'])

const open = () => {
  form.value = { levelName: '', status: '' }
  visible.value = true
}

defineExpose({ open })

const submit = () => {
  formRef.value.validate(async valid => {
    if (!valid) {
      ElMessage.warning('请填写完整信息')
      return
    }

    try {
      const res = await axios.post('/nursingLevel/addNursingLevel', null, {
        params: form.value
      })
      if (res.data.status === 200) {
        ElMessage.success('添加成功')
        visible.value = false
        emit('refresh')
      } else {
        ElMessage.error(res.data.msg || '添加失败')
      }
    } catch (e) {
      ElMessage.error('请求失败')
      console.error(e)
    }
  })
}
</script>

<style scoped>
.add-nursing-level-dialog ::v-deep(.el-dialog__title) {
  font-size: 27.2px;
  font-weight: bold;
}

.form-body {
  padding: 10px 0;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input,
.el-select {
  width: 100%;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding-top: 10px;
}
</style>
